package com.cj.tool

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.LinearLayout
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.cj.tool.utils.IPageHead
import com.cj.tool.utils.PageHead
import com.gyf.immersionbar.ktx.immersionBar
import kotlinx.android.synthetic.main.layout_title.*

abstract class BaseActivity : AppCompatActivity(), IPageHead {

    private lateinit var contentLayout: LinearLayout
    private lateinit var pageHead: PageHead

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       immersionBar {
            //statusBarColor(R.color.white)
            //navigationBarColor(R.color.colorPrimary)
            navigationBarEnable(false)
            if (isFitSystemWindows()) {
                fitsSystemWindows(true)
                statusBarDarkFont(true)
            }
        }
        initContentView()
        setContentView(layoutId())
        initView()
        setListener()
        
        
    }

    abstract fun layoutId(): Int
    abstract fun initView()
    abstract fun setListener()

    protected open fun isFitSystemWindows(): Boolean {
        return true
    }

    fun hideTitleBar() {
        layoutTitleBar?.isVisible = false
    }

    fun showTitleBar() {
        layoutTitleBar?.isVisible = true
    }

    open fun initContentView() {
        val viewGroup: ViewGroup = findViewById(android.R.id.content)
        viewGroup.removeAllViews()
        contentLayout = LinearLayout(this)
        contentLayout.orientation = LinearLayout.VERTICAL
        contentLayout.setBackgroundResource(R.color.white)
        viewGroup.addView(contentLayout)
        LayoutInflater.from(this).inflate(R.layout.layout_title, contentLayout, true)
    }

    override fun setContentView(@LayoutRes layoutResID: Int) {
        LayoutInflater.from(this).inflate(layoutResID, contentLayout, true)
    }

    var isEnableHideSoftInputFromWindow = true

    /**
     * 触摸空白区域自动隐藏键盘
     *
     * @param ev
     * @return
     */
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN && isEnableHideSoftInputFromWindow) {
            currentFocus?.apply {
                if (isShouldHideKeyboard(this, ev)) {
                    val imm = getSystemService(INPUT_METHOD_SERVICE) as? InputMethodManager
                    imm?.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    // 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘
    private fun isShouldHideKeyboard(v: View, event: MotionEvent): Boolean {
        if (v is EditText) {
            val l = intArrayOf(0, 0)
            v.getLocationInWindow(l)
            val left = l[0]
            val top = l[1]
            val bottom = top + v.getHeight()
            val right = left + v.getWidth()
            return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
        }
        return false
    }

    override fun getPageHead(
        activity: Activity?,
        listener: PageHead.OnPageHeadClickListener?
    ): PageHead {
        pageHead = PageHead(activity, listener)
        return pageHead
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}